package org.one.stone.soup.grfxML.plugin.text;

import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataStore;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.DataText;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.LockException;

public class Teletype extends SimplePlugin {

	public static final int ARG_SOURCE	=	0;
	public static final int ARG_SHOW	=	1;
	public static final int ARG_READY	=	2;
	public static final int ARG_HIDE	=	3;
	public static final int ARG_TARGET	=	4;

	DataStore source;		// data sources in pairs (boolean show,text message)

	DataState show	=	new DataState();	// boolean show flag -> out
	DataState ready	=	new DataState();	// boolean ready flag <- in
	DataState hide	=	new DataState();	// boolean hide flag -> out
	DataText target	=	new DataText();		// text area to populate

	DataText sourceText = new DataText();

	boolean typeing = false;
	boolean finishedTypeing = false;
	int maxLines = 2;
	int lineNo = 0;
	int offset = 0;
	int charNo = 0;
/**
 * ShowInfo constructor comment.
 */
public Teletype(GrfxMLEngine engine) {
	super(engine);
	source = new DataStore(engine);		// data sources in pairs (boolean show,text message)

}
/**
 * initialize method comment.
 */
public void initialize() {}
public void process()
{
  try{

	if(typeing==false)
	{
		for(int loop=0;loop<source.size();loop+=2)
		{
			if( ((DataState)source.get(loop)).getValue()==true )
			{
				show.setValue(true,this);
				sourceText = (DataText)source.get(loop+1);
				target.setEmpty();
				break;
			}
		}
	}

	hide.setValue(false,this);
	if(ready.getValue()==true)
	{
		show.setValue(false,this);
		lineNo=0;
		charNo=0;
		offset=0;
		typeing = true;
		finishedTypeing = false;
	}

	if(typeing==true)
	{
		if(charNo==0)
		{
			target.add(new DataString(),this);
		}

		if(lineNo>=sourceText.size())
		{
			finishedTypeing=true;
		}
		else
		{
			String data = sourceText.get(lineNo).getValue();
			if(charNo>=data.length())
			{
				if(lineNo>=maxLines)
				{
					target.remove(0,this);
					offset++;
				}

				lineNo++;
				charNo=0;
			}
			else
			{
				target.get(lineNo-offset).append(data.charAt(charNo));
				charNo++;
			}
		}
	}

	if(finishedTypeing==true)
	{
		finishedTypeing=false;
		typeing=false;
		hide.setValue(true,this);
	}
  }
  catch(LockException le){}//dummy catch
}
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	source	=	grfxMLCaster.cast(source,getArg(source,ARG_SOURCE,store));
	show	=	grfxMLCaster.cast(show,getArg(show,ARG_SHOW,store));
	ready	=	grfxMLCaster.cast(ready,getArg(ready,ARG_READY,store));
	hide	=	grfxMLCaster.cast(hide,getArg(hide,ARG_HIDE,store));
	target	=	grfxMLCaster.cast(target,getArg(target,ARG_TARGET,store));

/*	try{
		store.add(this,source);
		store.add(this,show);
		store.add(this,ready);
		store.add(this,hide);
		store.add(this,target);
	}catch(LockException le){}//dummy catch
*/
}
public void replace(org.one.stone.soup.mjdb.data.field.Field oldObj, org.one.stone.soup.mjdb.data.field.Field newObj)
{
	source	=	grfxMLCaster.replace(source,oldObj,newObj);
	show	=	grfxMLCaster.replace(show,oldObj,newObj);
	ready	=	grfxMLCaster.replace(ready,oldObj,newObj);
	hide	=	grfxMLCaster.replace(hide,oldObj,newObj);
	target	=	grfxMLCaster.replace(target,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
